package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.management.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Enid
 * @date 2025/9/29 10:31
 * @description 部门管理的控制器
 */
@Slf4j
@RestController
@RequestMapping("/depts")//路径延展
@SuppressWarnings("all")//忽略警告--一定要确定代码没有错误，这样会节约性能
public class DeptController {
    @Autowired
    private DeptService deptService;
    /**
     * 新增部门
     * @param dept
     */
    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        deptService.addDept(dept);
        return Result.success();
    }
    /**
     * 查询部门列表
     */
    @GetMapping
    public Result selectDepts(
           @RequestParam(name = "name",required = false) String name,
           @RequestParam(name = "status",required = false) Integer status,
           @RequestParam(name = "page",defaultValue = "1")Integer page,
           @RequestParam(name = "pageSize",defaultValue = "5") Integer pageSize
    ){
        log.info("查询部门列表，参数：部门名称={},状态={},当前页码={},每页大小={}",name,status,page,pageSize);
        PageResult<Dept> pageResult = deptService.selectDepts(name, status, page, pageSize);
        return Result.success(pageResult);
    }
    /**
     * 根据id查询部门
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") Integer id){
        log.info("根据id查询部门id为：{}",id);
        Dept dept = deptService.selectById(id);
        return Result.success(dept);
    }

    /**
     * 修改部门接口
     */
    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        log.info("修改部门id为：{}",dept.getId());
        deptService.updateDept(dept);
        return Result.success();
    }

    /**
     * 删除部门接口
     */
    @DeleteMapping("/{id}")
    public Result deleteDept(@PathVariable("id") Integer id){
        //TODO 测试异常处理
        //int i = 1/0;
        log.info("删除部门id为：{}",id);
        deptService.deleteDept(id);
        return Result.success();
    }
    /**
     * 查询所有部门接口
     */
    @GetMapping("/list")
    public Result selectAll(){
        List<Dept> deptList = deptService.selectAll();
        return Result.success(deptList);
    }
}
